/* GUI CONVERTED from ardrmti.p (converter v1.76) Wed Oct 30 20:10:19 2002 */
/* ardrmti.p - AR DEBIT/CREDIT MEMO MAINTENANCE - FOR TAX INCLUSIVE SCENARIOS */
/* Copyright 1986-2002 QAD Inc., Carpinteria, CA, USA.                        */
/* All rights reserved worldwide.  This is an unpublished work.               */
/*V8:ConvertMode=Maintenance                                                  */
/* $Revision: 1.4 $                                                           */

/******************************************************************************/

/* $Revision: 1.4 $    BY: Vinod Nair        DATE: 10/28/02 ECO: *N1W4*       */

/******************************************************************************/

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/

/******************************************************************************/
/* THIS PROGRAM PRORATES TAX AMONG AR DISTRIBUTION LINES AND CREATES A NEW    */
/* BALANCING DISTRIBUTION LINE OF TYPE "ti" FOR TAX INCLUSIVE MEMOS.          */
/* DISTRIBUTION LINE OF TYPE "ti" CANNOT BE MODIFIED.                         */
/******************************************************************************/

/* DISPLAY TITLE */
{mfdeclre.i}

define input  parameter ar_nbr           like ar_nbr        no-undo.
define input  parameter rndmthd          like rnd_rnd_mthd  no-undo.
define output parameter tot_tax_included like ar_amt        no-undo.

define variable         taxin_tran_amt   like ard_amt       no-undo.
define variable         tax_included     like ard_amt       no-undo.
define variable         tot_tax_used     as   decimal       no-undo.
define variable         tmpamt           as   decimal       no-undo.
define variable         mc-error-number  like msg_nbr       no-undo.

{gldydef.i}

define buffer           b_arddet         for  ard_det.

assign
   taxin_tran_amt   = 0
   tax_included     = 0
   tot_tax_included = 0
   tot_tax_used     = 0.

/* SELECT ALL TAX LINES FOR THIS MEMO */
for each tx2d_det
   fields (tx2d_cur_tax_amt tx2d_ref tx2d_tax_code
           tx2d_tax_in tx2d_totamt tx2d_tr_type)
   where tx2d_ref     = ar_nbr
   and   tx2d_tr_type = "18"
   no-lock
   break by tx2d_tax_code:
/*GUI*/ if global-beam-me-up then undo, leave.


   /* IF TAX INCLUDED IS YES */
   if tx2d_tax_in = yes
   then do:

      assign
         tot_tax_included  = tot_tax_included + tx2d_cur_tax_amt
         tax_included      = tax_included     + tx2d_cur_tax_amt
         taxin_tran_amt    = taxin_tran_amt   + tx2d_totamt.

      if last-of(tx2d_tax_code)
      then do:

         tot_tax_used      = 0.

         for first tx2_mstr
            fields (tx2_tax_code tx2_pt_taxc tx2_tax_usage)
            where tx2_tax_code = tx2d_tax_code
            no-lock:
         end. /* FOR FIRST tx2_mstr */

         /* PRO-RATE TAX AMONG ard_det's HAVING SAME */
         /* TAX CLASS AND TAX USAGE                  */
         for each ard_det
            where ard_nbr       =  ar_nbr
            and   ard_tax_at    =  "yes"
            and   ard_tax       <> "t"
            and   ard_tax       <> "ti"
            and   ard_taxc      =  tx2_pt_taxc
            and   ard_tax_usage =  tx2_tax_usage
            exclusive-lock
            break by ard_nbr:
/*GUI*/ if global-beam-me-up then undo, leave.


            tmpamt = ((ard_amt / taxin_tran_amt) * tax_included).

            /* ROUND tmpamt ACCORDING TO ROUNDING METHOD */
            {gprunp.i "mcpl" "p" "mc-curr-rnd"
               "(input-output tmpamt,
                 input rndmthd,
                 output mc-error-number)"}
            if mc-error-number <> 0
            then do:
               {pxmsg.i &MSGNUM=mc-error-number &ERRORLEVEL=3}.
            end. /* IF mc-error-number <> 0 */

            /* REDUCE ard_amt BY PRO-RATED TAX AMOUNT */
            find b_arddet
               where b_arddet.ard_nbr    = ar_nbr
               and   b_arddet.ard_ref    = ard_det.ard_ref
               and   b_arddet.ard_type   = ard_det.ard_type
               and   b_arddet.ard_tax_at = "no"
               and   b_arddet.ard_entity = ard_det.ard_entity
               and   b_arddet.ard_acct   = ard_det.ard_acct
               and   b_arddet.ard_sub    = ard_det.ard_sub
               and   b_arddet.ard_cc     = ard_det.ard_cc
               and   b_arddet.ard_tax    = "ti"
               exclusive-lock
               no-error.

            if not available b_arddet
            then do:

               create b_arddet.
               assign
                  b_arddet.ard_nbr       = ar_nbr
                  b_arddet.ard_acct      = ard_det.ard_acct
                  b_arddet.ard_sub       = ard_det.ard_sub
                  b_arddet.ard_cc        = ard_det.ard_cc
                  b_arddet.ard_entity    = ard_det.ard_entity
                  b_arddet.ard_project   = ard_det.ard_project
                  b_arddet.ard_dy_code   = dft-daybook
                  b_arddet.ard_type      = ard_det.ard_project
                  b_arddet.ard_taxc      = ard_det.ard_taxc
                  b_arddet.ard_tax_usage = ard_det.ard_tax_usage
                  b_arddet.ard_tax_at    = "no"
                  b_arddet.ard_tax       = "ti".

               for first ac_mstr
                  fields (ac_code ac_desc)
                  where ac_code = ard_det.ard_acct
                  no-lock:
                  b_arddet.ard_desc = ac_desc.
               end. /* FOR FIRST ac_mstr */

            end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* IF NOT AVAILABLE b_arddet */

            if last-of(ard_det.ard_nbr)
            then
               /* ADJUST THE ROUNDING DIFFERENCE */
               b_arddet.ard_amt = b_arddet.ard_amt - (tax_included
                                                      - tot_tax_used).
            else
               b_arddet.ard_amt = b_arddet.ard_amt - tmpamt.

            tot_tax_used = tot_tax_used + tmpamt.

         end. /* FOR EACH ard_det */

         assign
            taxin_tran_amt    = 0
            tax_included      = 0
            tot_tax_used      = 0.

      end.
/*GUI*/ if global-beam-me-up then undo, leave.
 /* IF LAST-OF(tx2d_tax_code) */

   end. /* IF tx2d_taxin = yes */

end. /* FOR EACH tx2d_det */
